<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Built-in template tags and filters | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/">go here</a>.
  
  </h2>
  <div class="section" id="s-built-in-template-tags-and-filters">
<span id="s-ref-templates-builtins"></span><span id="built-in-template-tags-and-filters"></span><span id="ref-templates-builtins"></span><h1>Built-in template tags and filters<a class="headerlink" href="#built-in-template-tags-and-filters" title="Permalink to this headline">¶</a></h1>
<p>This document describes Django’s built-in template tags and filters. It is
recommended that you use the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/templates/#template-built-in-reference"><em>automatic documentation</em></a>, if available, as this will also include
documentation for any custom tags or filters installed.</p>
<div class="section" id="s-built-in-tag-reference">
<span id="s-ref-templates-builtins-tags"></span><span id="built-in-tag-reference"></span><span id="ref-templates-builtins-tags"></span><h2>Built-in tag reference<a class="headerlink" href="#built-in-tag-reference" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-autoescape">
<span id="s-ttag-autoescape"></span><span id="autoescape"></span><span id="ttag-autoescape"></span><h3>autoescape<a class="headerlink" href="#autoescape" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Control the current auto-escaping behavior. This tag takes either <tt class="docutils literal"><span class="pre">on</span></tt> or
<tt class="docutils literal"><span class="pre">off</span></tt> as an argument and that determines whether auto-escaping is in effect
inside the block.</p>
<p>When auto-escaping is in effect, all variable content has HTML escaping applied
to it before placing the result into the output (but after any filters have
been applied). This is equivalent to manually applying the <tt class="docutils literal"><span class="pre">escape</span></tt> filter
to each variable.</p>
<p>The only exceptions are variables that are already marked as “safe” from
escaping, either by the code that populated the variable, or because it has had
the <tt class="docutils literal"><span class="pre">safe</span></tt> or <tt class="docutils literal"><span class="pre">escape</span></tt> filters applied.</p>
</div>
<div class="section" id="s-block">
<span id="s-ttag-block"></span><span id="block"></span><span id="ttag-block"></span><h3>block<a class="headerlink" href="#block" title="Permalink to this headline">¶</a></h3>
<p>Define a block that can be overridden by child templates. See
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/templates/#template-inheritance"><em>Template inheritance</em></a> for more information.</p>
</div>
<div class="section" id="s-comment">
<span id="s-ttag-comment"></span><span id="comment"></span><span id="ttag-comment"></span><h3>comment<a class="headerlink" href="#comment" title="Permalink to this headline">¶</a></h3>
<p>Ignore everything between <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">comment</span> <span class="pre">%}</span></tt> and <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">endcomment</span> <span class="pre">%}</span></tt></p>
</div>
<div class="section" id="s-cycle">
<span id="s-ttag-cycle"></span><span id="cycle"></span><span id="ttag-cycle"></span><h3>cycle<a class="headerlink" href="#cycle" title="Permalink to this headline">¶</a></h3>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> Cycle among the given strings or variables each time this tag is encountered.</div>
<p>Within a loop, cycles among the given strings each time through the
loop:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">&gt;</span>
        ...
    <span class="nt">&lt;/tr&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>You can use variables, too. For example, if you have two template variables,
<tt class="docutils literal"><span class="pre">rowvalue1</span></tt> and <tt class="docutils literal"><span class="pre">rowvalue2</span></tt>, you can cycle between their values like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowvalue1</span> <span class="nv">rowvalue2</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">&gt;</span>
        ...
    <span class="nt">&lt;/tr&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Yes, you can mix variables and strings:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="nv">rowvalue2</span> <span class="s1">'row3'</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">&gt;</span>
        ...
    <span class="nt">&lt;/tr&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In some cases you might want to refer to the next value of a cycle from
outside of a loop. To do this, just give the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag a name, using
"as", like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>From then on, you can insert the current value of the cycle wherever you'd like
in your template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">&gt;</span>...<span class="nt">&lt;/tr&gt;</span>
<span class="nt">&lt;tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">&gt;</span>...<span class="nt">&lt;/tr&gt;</span>
</pre></div>
</div>
<p>You can use any number of values in a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag, separated by spaces.
Values enclosed in single (<tt class="docutils literal"><span class="pre">'</span></tt>) or double quotes (<tt class="docutils literal"><span class="pre">"</span></tt>) are treated as
string literals, while values without quotes are treated as template variables.</p>
<p>For backwards compatibility, the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag supports the much inferior
old syntax from previous Django versions. You shouldn't use this in any new
projects, but for the sake of the people who are still using it, here's what it
looks like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">row1</span><span class="o">,</span><span class="nv">row2</span><span class="o">,</span><span class="nv">row3</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In this syntax, each value gets interpreted as a literal string, and there's no
way to specify variable values. Or literal commas. Or spaces. Did we mention
you shouldn't use this syntax in any new projects?</p>
</div>
<div class="section" id="s-debug">
<span id="s-ttag-debug"></span><span id="debug"></span><span id="ttag-debug"></span><h3>debug<a class="headerlink" href="#debug" title="Permalink to this headline">¶</a></h3>
<p>Output a whole load of debugging information, including the current context and
imported modules.</p>
</div>
<div class="section" id="s-extends">
<span id="s-ttag-extends"></span><span id="extends"></span><span id="ttag-extends"></span><h3>extends<a class="headerlink" href="#extends" title="Permalink to this headline">¶</a></h3>
<p>Signal that this template extends a parent template.</p>
<p>This tag can be used in two ways:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">"base.html"</span> <span class="pre">%}</span></tt> (with quotes) uses the literal value
<tt class="docutils literal"><span class="pre">"base.html"</span></tt> as the name of the parent template to extend.</li>
<li><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">variable</span> <span class="pre">%}</span></tt> uses the value of <tt class="docutils literal"><span class="pre">variable</span></tt>. If the variable
evaluates to a string, Django will use that string as the name of the
parent template. If the variable evaluates to a <tt class="docutils literal"><span class="pre">Template</span></tt> object,
Django will use that object as the parent template.</li>
</ul>
<p>See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/templates/#template-inheritance"><em>Template inheritance</em></a> for more information.</p>
</div>
<div class="section" id="s-filter">
<span id="s-ttag-filter"></span><span id="filter"></span><span id="ttag-filter"></span><h3>filter<a class="headerlink" href="#filter" title="Permalink to this headline">¶</a></h3>
<p>Filter the contents of the variable through variable filters.</p>
<p>Filters can also be piped through each other, and they can have arguments --
just like in variable syntax.</p>
<p>Sample usage:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">force_escape</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">%}</span>
    This text will be HTML-escaped, and will appear in all lowercase.
<span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-firstof">
<span id="s-ttag-firstof"></span><span id="firstof"></span><span id="ttag-firstof"></span><h3>firstof<a class="headerlink" href="#firstof" title="Permalink to this headline">¶</a></h3>
<p>Outputs the first variable passed that is not False.  Outputs nothing if all the
passed variables are False.</p>
<p>Sample usage:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This is equivalent to:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">var1</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var1</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}{%</span> <span class="k">if</span> <span class="nv">var2</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var2</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}{%</span> <span class="k">if</span> <span class="nv">var3</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var3</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>You can also use a literal string as a fallback value in case all
passed variables are False:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="s2">"fallback value"</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-for">
<span id="s-ttag-for"></span><span id="for"></span><span id="ttag-for"></span><h3>for<a class="headerlink" href="#for" title="Permalink to this headline">¶</a></h3>
<p>Loop over each item in an array.  For example, to display a list of athletes
provided in <tt class="docutils literal"><span class="pre">athlete_list</span></tt>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;ul&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
<p>You can loop over a list in reverse by using <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">obj</span> <span class="pre">in</span> <span class="pre">list</span> <span class="pre">reversed</span> <span class="pre">%}</span></tt>.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>If you need to loop over a list of lists, you can unpack the values
in each sub-list into individual variables. For example, if your context
contains a list of (x,y) coordinates called <tt class="docutils literal"><span class="pre">points</span></tt>, you could use the
following to output the list of points:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">x</span><span class="o">,</span> <span class="nv">y</span> <span class="k">in</span> <span class="nv">points</span> <span class="cp">%}</span>
    There is a point at <span class="cp">{{</span> <span class="nv">x</span> <span class="cp">}}</span>,<span class="cp">{{</span> <span class="nv">y</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This can also be useful if you need to access the items in a dictionary.
For example, if your context contained a dictionary <tt class="docutils literal"><span class="pre">data</span></tt>, the following
would display the keys and values of the dictionary:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">key</span><span class="o">,</span> <span class="nv">value</span> <span class="k">in</span> <span class="nv">data.items</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">key</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">value</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The for loop sets a number of variables available within the loop:</p>
<table class="docutils">
<colgroup>
<col width="35%">
<col width="65%">
</colgroup>
<thead valign="bottom">
<tr><th class="head">Variable</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">forloop.counter</span></tt></td>
<td>The current iteration of the loop (1-indexed)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.counter0</span></tt></td>
<td>The current iteration of the loop (0-indexed)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.revcounter</span></tt></td>
<td>The number of iterations from the end of the
loop (1-indexed)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.revcounter0</span></tt></td>
<td>The number of iterations from the end of the
loop (0-indexed)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.first</span></tt></td>
<td>True if this is the first time through the loop</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.last</span></tt></td>
<td>True if this is the last time through the loop</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">forloop.parentloop</span></tt></td>
<td>For nested loops, this is the loop "above" the
current one</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-if">
<span id="s-ttag-if"></span><span id="if"></span><span id="ttag-if"></span><h3>if<a class="headerlink" href="#if" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></tt> tag evaluates a variable, and if that variable is "true" (i.e.
exists, is not empty, and is not a false boolean value) the contents of the
block are output:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    Number of athletes: <span class="cp">{{</span> <span class="nv">athlete_list</span><span class="o">|</span><span class="nf">length</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
    No athletes.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In the above, if <tt class="docutils literal"><span class="pre">athlete_list</span></tt> is not empty, the number of athletes will be
displayed by the <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">athlete_list|length</span> <span class="pre">}}</span></tt> variable.</p>
<p>As you can see, the <tt class="docutils literal"><span class="pre">if</span></tt> tag can take an optional <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause that
will be displayed if the test fails.</p>
<p><tt class="docutils literal"><span class="pre">if</span></tt> tags may use <tt class="docutils literal"><span class="pre">and</span></tt>, <tt class="docutils literal"><span class="pre">or</span></tt> or <tt class="docutils literal"><span class="pre">not</span></tt> to test a number of variables or
to negate a given variable:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    Both athletes and coaches are available.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    There are no athletes.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes or some coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are no athletes or there are some coaches (OK, so
    writing English translations of boolean logic sounds
    stupid; it's not our fault).
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="k">not</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes and absolutely no coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">if</span></tt> tags don't allow <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt> clauses within the same tag, because
the order of logic would be ambiguous. For example, this is invalid:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="k">or</span> <span class="nv">cheerleader_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If you need to combine <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt> to do advanced logic, just use nested
<tt class="docutils literal"><span class="pre">if</span></tt> tags. For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">coach_list</span> <span class="k">or</span> <span class="nv">cheerleader_list</span> <span class="cp">%}</span>
        We have athletes, and either coaches or cheerleaders!
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Multiple uses of the same logical operator are fine, as long as you use the
same operator. For example, this is valid:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="k">or</span> <span class="nv">parent_list</span> <span class="k">or</span> <span class="nv">teacher_list</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ifchanged">
<span id="s-ttag-ifchanged"></span><span id="ifchanged"></span><span id="ttag-ifchanged"></span><h3>ifchanged<a class="headerlink" href="#ifchanged" title="Permalink to this headline">¶</a></h3>
<p>Check if a value has changed from the last iteration of a loop.</p>
<p>The 'ifchanged' block tag is used within a loop. It has two possible uses.</p>
<ol class="arabic">
<li><p class="first">Checks its own rendered contents against its previous state and only
displays the content if it has changed. For example, this displays a list of
days, only displaying the month if it changes:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>Archive for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}</span><span class="nt">&lt;/h1&gt;</span>

<span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="cp">%}</span><span class="nt">&lt;h3&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"F"</span> <span class="cp">}}</span><span class="nt">&lt;/h3&gt;</span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"M/d"</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="s">/"</span><span class="nt">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"j"</span> <span class="cp">}}</span><span class="nt">&lt;/a&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">If given a variable, check whether that variable has changed. For
example, the following shows the date every time it changes, but
only shows the hour if both the hour and the date has changed:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.date</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">date.date</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.hour</span> <span class="nv">date.date</span> <span class="cp">%}</span>
        <span class="cp">{{</span> <span class="nv">date.hour</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
</ol>
<p>The <tt class="docutils literal"><span class="pre">ifchanged</span></tt> tag can also take an optional <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause that
will be displayed if the value has not changed:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">match</span> <span class="k">in</span> <span class="nv">matches</span> <span class="cp">%}</span>
    <span class="nt">&lt;div</span> <span class="na">style=</span><span class="s">"background-color:</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">match.ballot_id</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s2">"red"</span> <span class="s2">"blue"</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            grey</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">    "</span><span class="nt">&gt;</span><span class="cp">{{</span> <span class="nv">match</span> <span class="cp">}}</span><span class="nt">&lt;/div&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ifequal">
<span id="s-ttag-ifequal"></span><span id="ifequal"></span><span id="ttag-ifequal"></span><h3>ifequal<a class="headerlink" href="#ifequal" title="Permalink to this headline">¶</a></h3>
<p>Output the contents of the block if the two arguments equal each other.</p>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ifequal</span> <span class="nv">user.id</span> <span class="nv">comment.user_id</span> <span class="cp">%}</span>
    ...
<span class="cp">{%</span> <span class="k">endifequal</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>As in the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></tt> tag, an <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause is optional.</p>
<p>The arguments can be hard-coded strings, so the following is valid:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ifequal</span> <span class="nv">user.username</span> <span class="s2">"adrian"</span> <span class="cp">%}</span>
    ...
<span class="cp">{%</span> <span class="k">endifequal</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>It is only possible to compare an argument to template variables or strings.
You cannot check for equality with Python objects such as <tt class="xref docutils literal"><span class="pre">True</span></tt> or
<tt class="xref docutils literal"><span class="pre">False</span></tt>.  If you need to test if something is true or false, use the <tt class="docutils literal"><span class="pre">if</span></tt>
tag instead.</p>
</div>
<div class="section" id="s-ifnotequal">
<span id="s-ttag-ifnotequal"></span><span id="ifnotequal"></span><span id="ttag-ifnotequal"></span><h3>ifnotequal<a class="headerlink" href="#ifnotequal" title="Permalink to this headline">¶</a></h3>
<p>Just like <tt class="docutils literal"><span class="pre">ifequal</span></tt>, except it tests that the two arguments are not equal.</p>
</div>
<div class="section" id="s-include">
<span id="s-ttag-include"></span><span id="include"></span><span id="ttag-include"></span><h3>include<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3>
<p>Loads a template and renders it with the current context. This is a way of
"including" other templates within a template.</p>
<p>The template name can either be a variable or a hard-coded (quoted) string,
in either single or double quotes.</p>
<p>This example includes the contents of the template <tt class="docutils literal"><span class="pre">"foo/bar.html"</span></tt>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"foo/bar.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This example includes the contents of the template whose name is contained in
the variable <tt class="docutils literal"><span class="pre">template_name</span></tt>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="nv">template_name</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>An included template is rendered with the context of the template that's
including it. This example produces the output <tt class="docutils literal"><span class="pre">"Hello,</span> <span class="pre">John"</span></tt>:</p>
<ul>
<li><p class="first">Context: variable <tt class="docutils literal"><span class="pre">person</span></tt> is set to <tt class="docutils literal"><span class="pre">"john"</span></tt>.</p>
</li>
<li><p class="first">Template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"name_snippet.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">name_snippet.html</span></tt> template:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Hello, <span class="cp">{{</span> <span class="nv">person</span> <span class="cp">}}</span>
</pre></div>
</div>
</li>
</ul>
<p>See also: <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt>.</p>
</div>
<div class="section" id="s-load">
<span id="s-ttag-load"></span><span id="load"></span><span id="ttag-load"></span><h3>load<a class="headerlink" href="#load" title="Permalink to this headline">¶</a></h3>
<p>Load a custom template tag set.</p>
<p>See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/howto/custom-template-tags/#howto-custom-template-tags"><em>Custom tag and filter libraries</em></a> for more information.</p>
</div>
<div class="section" id="s-now">
<span id="s-ttag-now"></span><span id="now"></span><span id="ttag-now"></span><h3>now<a class="headerlink" href="#now" title="Permalink to this headline">¶</a></h3>
<p>Display the date, formatted according to the given string.</p>
<p>Uses the same format as PHP's <tt class="docutils literal"><span class="pre">date()</span></tt> function (<a class="reference external" href="http://php.net/date">http://php.net/date</a>)
with some custom extensions.</p>
<p>Available format strings:</p>
<table class="docutils">
<colgroup>
<col width="12%">
<col width="31%">
<col width="57%">
</colgroup>
<thead valign="bottom">
<tr><th class="head">Format character</th>
<th class="head">Description</th>
<th class="head">Example output</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>a</td>
<td><tt class="docutils literal"><span class="pre">'a.m.'</span></tt> or <tt class="docutils literal"><span class="pre">'p.m.'</span></tt> (Note that
this is slightly different than PHP's
output, because this includes periods
to match Associated Press style.)</td>
<td><tt class="docutils literal"><span class="pre">'a.m.'</span></tt></td>
</tr>
<tr><td>A</td>
<td><tt class="docutils literal"><span class="pre">'AM'</span></tt> or <tt class="docutils literal"><span class="pre">'PM'</span></tt>.</td>
<td><tt class="docutils literal"><span class="pre">'AM'</span></tt></td>
</tr>
<tr><td>b</td>
<td>Month, textual, 3 letters, lowercase.</td>
<td><tt class="docutils literal"><span class="pre">'jan'</span></tt></td>
</tr>
<tr><td>B</td>
<td>Not implemented.</td>
<td>&nbsp;</td>
</tr>
<tr><td>d</td>
<td>Day of the month, 2 digits with
leading zeros.</td>
<td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'31'</span></tt></td>
</tr>
<tr><td>D</td>
<td>Day of the week, textual, 3 letters.</td>
<td><tt class="docutils literal"><span class="pre">'Fri'</span></tt></td>
</tr>
<tr><td>f</td>
<td>Time, in 12-hour hours and minutes,
with minutes left off if they're zero.
Proprietary extension.</td>
<td><tt class="docutils literal"><span class="pre">'1'</span></tt>, <tt class="docutils literal"><span class="pre">'1:30'</span></tt></td>
</tr>
<tr><td>F</td>
<td>Month, textual, long.</td>
<td><tt class="docutils literal"><span class="pre">'January'</span></tt></td>
</tr>
<tr><td>g</td>
<td>Hour, 12-hour format without leading
zeros.</td>
<td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td>
</tr>
<tr><td>G</td>
<td>Hour, 24-hour format without leading
zeros.</td>
<td><tt class="docutils literal"><span class="pre">'0'</span></tt> to <tt class="docutils literal"><span class="pre">'23'</span></tt></td>
</tr>
<tr><td>h</td>
<td>Hour, 12-hour format.</td>
<td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td>
</tr>
<tr><td>H</td>
<td>Hour, 24-hour format.</td>
<td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'23'</span></tt></td>
</tr>
<tr><td>i</td>
<td>Minutes.</td>
<td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'59'</span></tt></td>
</tr>
<tr><td>I</td>
<td>Not implemented.</td>
<td>&nbsp;</td>
</tr>
<tr><td>j</td>
<td>Day of the month without leading
zeros.</td>
<td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'31'</span></tt></td>
</tr>
<tr><td>l</td>
<td>Day of the week, textual, long.</td>
<td><tt class="docutils literal"><span class="pre">'Friday'</span></tt></td>
</tr>
<tr><td>L</td>
<td>Boolean for whether it's a leap year.</td>
<td><tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt></td>
</tr>
<tr><td>m</td>
<td>Month, 2 digits with leading zeros.</td>
<td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td>
</tr>
<tr><td>M</td>
<td>Month, textual, 3 letters.</td>
<td><tt class="docutils literal"><span class="pre">'Jan'</span></tt></td>
</tr>
<tr><td>n</td>
<td>Month without leading zeros.</td>
<td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td>
</tr>
<tr><td>N</td>
<td>Month abbreviation in Associated Press
style. Proprietary extension.</td>
<td><tt class="docutils literal"><span class="pre">'Jan.'</span></tt>, <tt class="docutils literal"><span class="pre">'Feb.'</span></tt>, <tt class="docutils literal"><span class="pre">'March'</span></tt>, <tt class="docutils literal"><span class="pre">'May'</span></tt></td>
</tr>
<tr><td>O</td>
<td>Difference to Greenwich time in hours.</td>
<td><tt class="docutils literal"><span class="pre">'+0200'</span></tt></td>
</tr>
<tr><td>P</td>
<td>Time, in 12-hour hours, minutes and
'a.m.'/'p.m.', with minutes left off
if they're zero and the special-case
strings 'midnight' and 'noon' if
appropriate. Proprietary extension.</td>
<td><tt class="docutils literal"><span class="pre">'1</span> <span class="pre">a.m.'</span></tt>, <tt class="docutils literal"><span class="pre">'1:30</span> <span class="pre">p.m.'</span></tt>, <tt class="docutils literal"><span class="pre">'midnight'</span></tt>, <tt class="docutils literal"><span class="pre">'noon'</span></tt>, <tt class="docutils literal"><span class="pre">'12:30</span> <span class="pre">p.m.'</span></tt></td>
</tr>
<tr><td>r</td>
<td>RFC 2822 formatted date.</td>
<td><tt class="docutils literal"><span class="pre">'Thu,</span> <span class="pre">21</span> <span class="pre">Dec</span> <span class="pre">2000</span> <span class="pre">16:01:07</span> <span class="pre">+0200'</span></tt></td>
</tr>
<tr><td>s</td>
<td>Seconds, 2 digits with leading zeros.</td>
<td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'59'</span></tt></td>
</tr>
<tr><td>S</td>
<td>English ordinal suffix for day of the
month, 2 characters.</td>
<td><tt class="docutils literal"><span class="pre">'st'</span></tt>, <tt class="docutils literal"><span class="pre">'nd'</span></tt>, <tt class="docutils literal"><span class="pre">'rd'</span></tt> or <tt class="docutils literal"><span class="pre">'th'</span></tt></td>
</tr>
<tr><td>t</td>
<td>Number of days in the given month.</td>
<td><tt class="docutils literal"><span class="pre">28</span></tt> to <tt class="docutils literal"><span class="pre">31</span></tt></td>
</tr>
<tr><td>T</td>
<td>Time zone of this machine.</td>
<td><tt class="docutils literal"><span class="pre">'EST'</span></tt>, <tt class="docutils literal"><span class="pre">'MDT'</span></tt></td>
</tr>
<tr><td>U</td>
<td>Not implemented.</td>
<td>&nbsp;</td>
</tr>
<tr><td>w</td>
<td>Day of the week, digits without
leading zeros.</td>
<td><tt class="docutils literal"><span class="pre">'0'</span></tt> (Sunday) to <tt class="docutils literal"><span class="pre">'6'</span></tt> (Saturday)</td>
</tr>
<tr><td>W</td>
<td>ISO-8601 week number of year, with
weeks starting on Monday.</td>
<td><tt class="docutils literal"><span class="pre">1</span></tt>, <tt class="docutils literal"><span class="pre">53</span></tt></td>
</tr>
<tr><td>y</td>
<td>Year, 2 digits.</td>
<td><tt class="docutils literal"><span class="pre">'99'</span></tt></td>
</tr>
<tr><td>Y</td>
<td>Year, 4 digits.</td>
<td><tt class="docutils literal"><span class="pre">'1999'</span></tt></td>
</tr>
<tr><td>z</td>
<td>Day of the year.</td>
<td><tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">365</span></tt></td>
</tr>
<tr><td>Z</td>
<td>Time zone offset in seconds. The
offset for timezones west of UTC is
always negative, and for those east of
UTC is always positive.</td>
<td><tt class="docutils literal"><span class="pre">-43200</span></tt> to <tt class="docutils literal"><span class="pre">43200</span></tt></td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS F Y H:i"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Note that you can backslash-escape a format string if you want to use the
"raw" value. In this example, "f" is backslash-escaped, because otherwise
"f" is a format string that displays the time. The "o" doesn't need to be
escaped, because it's not a format character:</p>
<div class="highlight-html+django"><div class="highlight"><pre>It is the <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS o\f F"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This would display as "It is the 4th of September".</p>
</div>
<div class="section" id="s-regroup">
<span id="s-ttag-regroup"></span><span id="regroup"></span><span id="ttag-regroup"></span><h3>regroup<a class="headerlink" href="#regroup" title="Permalink to this headline">¶</a></h3>
<p>Regroup a list of alike objects by a common attribute.</p>
<p>This complex tag is best illustrated by use of an example: say that <tt class="docutils literal"><span class="pre">people</span></tt>
is a list of people represented by dictionaries with <tt class="docutils literal"><span class="pre">first_name</span></tt>,
<tt class="docutils literal"><span class="pre">last_name</span></tt>, and <tt class="docutils literal"><span class="pre">gender</span></tt> keys:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">people</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'George'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Bush'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Bill'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Clinton'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Margaret'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Thatcher'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Condoleezza'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Rice'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Pat'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Smith'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Unknown'</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>...and you'd like to display a hierarchical list that is ordered by gender,
like this:</p>
<ul>
<li><dl class="first docutils">
<dt>Male:</dt>
<dd><ul class="first last simple">
<li>George Bush</li>
<li>Bill Clinton</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Female:</dt>
<dd><ul class="first last simple">
<li>Margaret Thatcher</li>
<li>Condoleezza Rice</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Unknown:</dt>
<dd><ul class="first last simple">
<li>Pat Smith</li>
</ul>
</dd>
</dl>
</li>
</ul>
<p>You can use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> tag to group the list of people by gender.
The following snippet of template code would accomplish this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">people</span> <span class="nv">by</span> <span class="nv">gender</span> <span class="k">as</span> <span class="nv">gender_list</span> <span class="cp">%}</span>

<span class="nt">&lt;ul&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">gender</span> <span class="k">in</span> <span class="nv">gender_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">gender.grouper</span> <span class="cp">}}</span>
    <span class="nt">&lt;ul&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">gender.list</span> <span class="cp">%}</span>
        <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">item.first_name</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">item.last_name</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="nt">&lt;/ul&gt;</span>
    <span class="nt">&lt;/li&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
<p>Let's walk through this example. <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> takes three arguments: the
list you want to regroup, the attribute to group by, and the name of the
resulting list. Here, we're regrouping the <tt class="docutils literal"><span class="pre">people</span></tt> list by the <tt class="docutils literal"><span class="pre">gender</span></tt>
attribute and calling the result <tt class="docutils literal"><span class="pre">gender_list</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> produces a list (in this case, <tt class="docutils literal"><span class="pre">gender_list</span></tt>) of
<strong>group objects</strong>. Each group object has two attributes:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">grouper</span></tt> -- the item that was grouped by (e.g., the string "Male" or
"Female").</li>
<li><tt class="docutils literal"><span class="pre">list</span></tt> -- a list of all items in this group (e.g., a list of all people
with gender='Male').</li>
</ul>
<p>Note that <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> does not order its input! Our example relies on
the fact that the <tt class="docutils literal"><span class="pre">people</span></tt> list was ordered by <tt class="docutils literal"><span class="pre">gender</span></tt> in the first place.
If the <tt class="docutils literal"><span class="pre">people</span></tt> list did <em>not</em> order its members by <tt class="docutils literal"><span class="pre">gender</span></tt>, the regrouping
would naively display more than one group for a single gender. For example,
say the <tt class="docutils literal"><span class="pre">people</span></tt> list was set to this (note that the males are not grouped
together):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">people</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Bill'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Clinton'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Pat'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Smith'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Unknown'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Margaret'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Thatcher'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'George'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Bush'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Condoleezza'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Rice'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>With this input for <tt class="docutils literal"><span class="pre">people</span></tt>, the example <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> template code
above would result in the following output:</p>
<ul>
<li><dl class="first docutils">
<dt>Male:</dt>
<dd><ul class="first last simple">
<li>Bill Clinton</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Unknown:</dt>
<dd><ul class="first last simple">
<li>Pat Smith</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Female:</dt>
<dd><ul class="first last simple">
<li>Margaret Thatcher</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Male:</dt>
<dd><ul class="first last simple">
<li>George Bush</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Female:</dt>
<dd><ul class="first last simple">
<li>Condoleezza Rice</li>
</ul>
</dd>
</dl>
</li>
</ul>
<p>The easiest solution to this gotcha is to make sure in your view code that the
data is ordered according to how you want to display it.</p>
<p>Another solution is to sort the data in the template using the <tt class="docutils literal"><span class="pre">dictsort</span></tt>
filter, if your data is in a list of dictionaries:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">people</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"gender"</span> <span class="nv">by</span> <span class="nv">gender</span> <span class="k">as</span> <span class="nv">gender_list</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-spaceless">
<span id="s-ttag-spaceless"></span><span id="spaceless"></span><span id="ttag-spaceless"></span><h3>spaceless<a class="headerlink" href="#spaceless" title="Permalink to this headline">¶</a></h3>
<p>Removes whitespace between HTML tags. This includes tab
characters and newlines.</p>
<p>Example usage:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="nt">&lt;p&gt;</span>
        <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"foo/"</span><span class="nt">&gt;</span>Foo<span class="nt">&lt;/a&gt;</span>
    <span class="nt">&lt;/p&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This example would return this HTML:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;p&gt;&lt;a</span> <span class="na">href=</span><span class="s">"foo/"</span><span class="nt">&gt;</span>Foo<span class="nt">&lt;/a&gt;&lt;/p&gt;</span>
</pre></div>
</div>
<p>Only space between <em>tags</em> is removed -- not space between tags and text. In
this example, the space around <tt class="docutils literal"><span class="pre">Hello</span></tt> won't be stripped:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="nt">&lt;strong&gt;</span>
        Hello
    <span class="nt">&lt;/strong&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ssi">
<span id="s-ttag-ssi"></span><span id="ssi"></span><span id="ttag-ssi"></span><h3>ssi<a class="headerlink" href="#ssi" title="Permalink to this headline">¶</a></h3>
<p>Output the contents of a given file into the page.</p>
<p>Like a simple "include" tag, <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt> includes the contents of another
file -- which must be specified using an absolute path -- in the current
page:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ssi</span> <span class="o">/</span><span class="nv">home</span><span class="o">/</span><span class="nv">html</span><span class="o">/</span><span class="nv">ljworld.com</span><span class="o">/</span><span class="nv">includes</span><span class="o">/</span><span class="nv">right_generic.html</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If the optional "parsed" parameter is given, the contents of the included
file are evaluated as template code, within the current context:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ssi</span> <span class="o">/</span><span class="nv">home</span><span class="o">/</span><span class="nv">html</span><span class="o">/</span><span class="nv">ljworld.com</span><span class="o">/</span><span class="nv">includes</span><span class="o">/</span><span class="nv">right_generic.html</span> <span class="nv">parsed</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Note that if you use <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt>, you'll need to define
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-ALLOWED_INCLUDE_ROOTS"><tt class="xref docutils literal"><span class="pre">ALLOWED_INCLUDE_ROOTS</span></tt></a> in your Django settings, as a security measure.</p>
<p>See also: <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">include</span> <span class="pre">%}</span></tt>.</p>
</div>
<div class="section" id="s-templatetag">
<span id="s-ttag-templatetag"></span><span id="templatetag"></span><span id="ttag-templatetag"></span><h3>templatetag<a class="headerlink" href="#templatetag" title="Permalink to this headline">¶</a></h3>
<p>Output one of the syntax characters used to compose template tags.</p>
<p>Since the template system has no concept of "escaping", to display one of the
bits used in template tags, you must use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">templatetag</span> <span class="pre">%}</span></tt> tag.</p>
<p>The argument tells which template bit to output:</p>
<table class="docutils">
<colgroup>
<col width="72%">
<col width="28%">
</colgroup>
<thead valign="bottom">
<tr><th class="head">Argument</th>
<th class="head">Outputs</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">openblock</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{%</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">closeblock</span></tt></td>
<td><tt class="docutils literal"><span class="pre">%}</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">openvariable</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">closevariable</span></tt></td>
<td><tt class="docutils literal"><span class="pre">}}</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">openbrace</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">closebrace</span></tt></td>
<td><tt class="docutils literal"><span class="pre">}</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">opencomment</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{#</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">closecomment</span></tt></td>
<td><tt class="docutils literal"><span class="pre">#}</span></tt></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-url">
<span id="s-ttag-url"></span><span id="url"></span><span id="ttag-url"></span><h3>url<a class="headerlink" href="#url" title="Permalink to this headline">¶</a></h3>
<p>Returns an absolute URL (i.e., a URL without the domain name) matching a given
view function and optional parameters. This is a way to output links without
violating the DRY principle by having to hard-code URLs in your templates:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.some_view</span> <span class="nv">arg1</span><span class="o">,</span><span class="nv">arg2</span><span class="o">,</span><span class="nv">name1</span><span class="o">=</span><span class="nv">value1</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The first argument is a path to a view function in the format
<tt class="docutils literal"><span class="pre">package.package.module.function</span></tt>. Additional arguments are optional and
should be comma-separated values that will be used as positional and keyword
arguments in the URL. All arguments required by the URLconf should be present.</p>
<p>For example, suppose you have a view, <tt class="docutils literal"><span class="pre">app_views.client</span></tt>, whose URLconf
takes a client ID (here, <tt class="docutils literal"><span class="pre">client()</span></tt> is a method inside the views file
<tt class="docutils literal"><span class="pre">app_views.py</span></tt>). The URLconf line might look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">'^client/(\d+)/$'</span><span class="p">,</span> <span class="s">'app_views.client'</span><span class="p">)</span>
</pre></div>
</div>
<p>If this app's URLconf is included into the project's URLconf under a path
such as this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">'^clients/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'project_name.app_name.urls'</span><span class="p">))</span>
</pre></div>
</div>
<p>...then, in a template, you can create a link to this view like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">app_views.client</span> <span class="nv">client.id</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The template tag will output the string <tt class="docutils literal"><span class="pre">/clients/client/123/</span></tt>.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>If you're using <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/http/urls/#naming-url-patterns"><em>named URL patterns</em></a>, you can
refer to the name of the pattern in the <tt class="docutils literal"><span class="pre">url</span></tt> tag instead of using the
path to the view.</p>
<p>Note that if the URL you're reversing doesn't exist, you'll get an
<tt class="xref docutils literal"><span class="pre">NoReverseMatch</span></tt> exception raised, which will cause your site to display an
error page.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>If you'd like to retrieve a URL without displaying it, you can use a slightly
different call:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</span> <span class="nv">arg</span><span class="o">,</span> <span class="nv">arg2</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>

<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">&gt;</span>I'm linking to <span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="nt">&lt;/a&gt;</span>
</pre></div>
</div>
<p>This <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">...</span> <span class="pre">as</span> <span class="pre">var</span> <span class="pre">%}</span></tt> syntax will <em>not</em> cause an error if the view is
missing. In practice you'll use this to link to views that are optional:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">the_url</span> <span class="cp">%}</span>
  <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">&gt;</span>Link to optional stuff<span class="nt">&lt;/a&gt;</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-widthratio">
<span id="s-ttag-widthratio"></span><span id="widthratio"></span><span id="ttag-widthratio"></span><h3>widthratio<a class="headerlink" href="#widthratio" title="Permalink to this headline">¶</a></h3>
<p>For creating bar charts and such, this tag calculates the ratio of a given value
to a maximum value, and then applies that ratio to a constant.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;img</span> <span class="na">src=</span><span class="s">"bar.gif"</span> <span class="na">height=</span><span class="s">"10"</span> <span class="na">width=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="m">100</span> <span class="cp">%}</span><span class="s">"</span> <span class="nt">/&gt;</span>
</pre></div>
</div>
<p>Above, if <tt class="docutils literal"><span class="pre">this_value</span></tt> is 175 and <tt class="docutils literal"><span class="pre">max_value</span></tt> is 200, the image in the
above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5
which is rounded up to 88).</p>
</div>
<div class="section" id="s-with">
<span id="s-ttag-with"></span><span id="with"></span><span id="ttag-with"></span><h3>with<a class="headerlink" href="#with" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Caches a complex variable under a simpler name. This is useful when accessing
an "expensive" method (e.g., one that hits the database) multiple times.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">with</span> <span class="nv">business.employees.count</span> <span class="k">as</span> <span class="nv">total</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">total</span> <span class="cp">}}</span> employee<span class="cp">{{</span> <span class="nv">total</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The populated variable (in the example above, <tt class="docutils literal"><span class="pre">total</span></tt>) is only available
between the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">%}</span></tt> and <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">endwith</span> <span class="pre">%}</span></tt> tags.</p>
</div>
</div>
<div class="section" id="s-built-in-filter-reference">
<span id="s-ref-templates-builtins-filters"></span><span id="built-in-filter-reference"></span><span id="ref-templates-builtins-filters"></span><h2>Built-in filter reference<a class="headerlink" href="#built-in-filter-reference" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-add">
<span id="s-tfilter-add"></span><span id="add"></span><span id="tfilter-add"></span><h3>add<a class="headerlink" href="#add" title="Permalink to this headline">¶</a></h3>
<p>Adds the argument to the value.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">add</span><span class="s2">:"2"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">4</span></tt>, then the output will be <tt class="docutils literal"><span class="pre">6</span></tt>.</p>
</div>
<div class="section" id="s-addslashes">
<span id="s-tfilter-addslashes"></span><span id="addslashes"></span><span id="tfilter-addslashes"></span><h3>addslashes<a class="headerlink" href="#addslashes" title="Permalink to this headline">¶</a></h3>
<p>Adds slashes before quotes. Useful for escaping strings in CSV, for example.</p>
</div>
<div class="section" id="s-capfirst">
<span id="s-tfilter-capfirst"></span><span id="capfirst"></span><span id="tfilter-capfirst"></span><h3>capfirst<a class="headerlink" href="#capfirst" title="Permalink to this headline">¶</a></h3>
<p>Capitalizes the first character of the value.</p>
</div>
<div class="section" id="s-center">
<span id="s-tfilter-center"></span><span id="center"></span><span id="tfilter-center"></span><h3>center<a class="headerlink" href="#center" title="Permalink to this headline">¶</a></h3>
<p>Centers the value in a field of a given width.</p>
</div>
<div class="section" id="s-cut">
<span id="s-tfilter-cut"></span><span id="cut"></span><span id="tfilter-cut"></span><h3>cut<a class="headerlink" href="#cut" title="Permalink to this headline">¶</a></h3>
<p>Removes all values of arg from the given string.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">cut</span><span class="s2">:" "</span><span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"String</span> <span class="pre">with</span> <span class="pre">spaces"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Stringwithspaces"</span></tt>.</p>
</div>
<div class="section" id="s-date">
<span id="s-tfilter-date"></span><span id="date"></span><span id="tfilter-date"></span><h3>date<a class="headerlink" href="#date" title="Permalink to this headline">¶</a></h3>
<p>Formats a date according to the given format (same as the <a class="reference internal" href="#now">now</a> tag).</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"D d M Y"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is a <tt class="docutils literal"><span class="pre">datetime</span></tt> object (e.g., the result of
<tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt>), the output will be the string
<tt class="docutils literal"><span class="pre">'Wed</span> <span class="pre">09</span> <span class="pre">Jan</span> <span class="pre">2008'</span></tt>.</p>
<p>When used without a format string:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>...the formatting string defined in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-DATE_FORMAT"><tt class="xref docutils literal"><span class="pre">DATE_FORMAT</span></tt></a> setting will be
used.</p>
</div>
<div class="section" id="s-default">
<span id="s-tfilter-default"></span><span id="default"></span><span id="tfilter-default"></span><h3>default<a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3>
<p>If value evaluates to <tt class="xref docutils literal"><span class="pre">False</span></tt>, use given default. Otherwise, use the value.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default</span><span class="s2">:"nothing"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">""</span></tt> (the empty string), the output will be <tt class="docutils literal"><span class="pre">nothing</span></tt>.</p>
</div>
<div class="section" id="s-default-if-none">
<span id="s-tfilter-default_if_none"></span><span id="default-if-none"></span><span id="tfilter-default_if_none"></span><h3>default_if_none<a class="headerlink" href="#default-if-none" title="Permalink to this headline">¶</a></h3>
<p>If (and only if) value is <tt class="xref docutils literal"><span class="pre">None</span></tt>, use given default. Otherwise, use the
value.</p>
<p>Note that if an empty string is given, the default value will <em>not</em> be used.
Use the <tt class="docutils literal"><span class="pre">default</span></tt> filter if you want to fallback for empty strings.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default_if_none</span><span class="s2">:"nothing"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, the output will be the string <tt class="docutils literal"><span class="pre">"nothing"</span></tt>.</p>
</div>
<div class="section" id="s-dictsort">
<span id="s-tfilter-dictsort"></span><span id="dictsort"></span><span id="tfilter-dictsort"></span><h3>dictsort<a class="headerlink" href="#dictsort" title="Permalink to this headline">¶</a></h3>
<p>Takes a list of dictionaries and returns that list sorted by the key given in
the argument.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"name"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'zed'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">19</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'amy'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'joe'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">31</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>then the output would be:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'amy'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'joe'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">31</span><span class="p">},</span>
    <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'zed'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mf">19</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-dictsortreversed">
<span id="s-tfilter-dictsortreversed"></span><span id="dictsortreversed"></span><span id="tfilter-dictsortreversed"></span><h3>dictsortreversed<a class="headerlink" href="#dictsortreversed" title="Permalink to this headline">¶</a></h3>
<p>Takes a list of dictionaries and returns that list sorted in reverse order by
the key given in the argument. This works exactly the same as the above filter,
but the returned value will be in reverse order.</p>
</div>
<div class="section" id="s-divisibleby">
<span id="s-tfilter-divisibleby"></span><span id="divisibleby"></span><span id="tfilter-divisibleby"></span><h3>divisibleby<a class="headerlink" href="#divisibleby" title="Permalink to this headline">¶</a></h3>
<p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the value is divisible by the argument.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">divisibleby</span><span class="s2">:"3"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">21</span></tt>, the output would be <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="section" id="s-escape">
<span id="s-tfilter-escape"></span><span id="escape"></span><span id="tfilter-escape"></span><h3>escape<a class="headerlink" href="#escape" title="Permalink to this headline">¶</a></h3>
<p>Escapes a string's HTML. Specifically, it makes these replacements:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">&lt;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;lt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&gt;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;gt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">'</span></tt> (single quote) is converted to <tt class="docutils literal"><span class="pre">&amp;#39;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">"</span></tt> (double quote) is converted to <tt class="docutils literal"><span class="pre">&amp;quot;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&amp;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;amp;</span></tt></li>
</ul>
<p>The escaping is only applied when the string is output, so it does not matter
where in a chained sequence of filters you put <tt class="docutils literal"><span class="pre">escape</span></tt>: it will always be
applied as though it were the last filter. If you want escaping to be applied
immediately, use the <tt class="docutils literal"><span class="pre">force_escape</span></tt> filter.</p>
<p>Applying <tt class="docutils literal"><span class="pre">escape</span></tt> to a variable that would normally have auto-escaping
applied to the result will only result in one round of escaping being done. So
it is safe to use this function even in auto-escaping environments. If you want
multiple escaping passes to be applied, use the <tt class="docutils literal"><span class="pre">force_escape</span></tt> filter.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> Due to auto-escaping, the behavior of this filter has changed slightly.
The replacements are only made once, after
all other filters are applied -- including filters before and after it.</div>
</div>
<div class="section" id="s-escapejs">
<span id="s-tfilter-escapejs"></span><span id="escapejs"></span><span id="tfilter-escapejs"></span><h3>escapejs<a class="headerlink" href="#escapejs" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Escapes characters for use in JavaScript strings. This does <em>not</em> make the
string safe for use in HTML, but does protect you from syntax errors when using
templates to generate JavaScript/JSON.</p>
</div>
<div class="section" id="s-filesizeformat">
<span id="s-tfilter-filesizeformat"></span><span id="filesizeformat"></span><span id="tfilter-filesizeformat"></span><h3>filesizeformat<a class="headerlink" href="#filesizeformat" title="Permalink to this headline">¶</a></h3>
<p>Format the value like a 'human-readable' file size (i.e. <tt class="docutils literal"><span class="pre">'13</span> <span class="pre">KB'</span></tt>,
<tt class="docutils literal"><span class="pre">'4.1</span> <span class="pre">MB'</span></tt>, <tt class="docutils literal"><span class="pre">'102</span> <span class="pre">bytes'</span></tt>, etc).</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">filesizeformat</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is 123456789, the output would be <tt class="docutils literal"><span class="pre">117.7</span> <span class="pre">MB</span></tt>.</p>
</div>
<div class="section" id="s-first">
<span id="s-tfilter-first"></span><span id="first"></span><span id="tfilter-first"></span><h3>first<a class="headerlink" href="#first" title="Permalink to this headline">¶</a></h3>
<p>Returns the first item in a list.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">first</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></tt>, the output will be <tt class="docutils literal"><span class="pre">'a'</span></tt>.</p>
</div>
<div class="section" id="s-fix-ampersands">
<span id="s-tfilter-fix_ampersands"></span><span id="fix-ampersands"></span><span id="tfilter-fix_ampersands"></span><h3>fix_ampersands<a class="headerlink" href="#fix-ampersands" title="Permalink to this headline">¶</a></h3>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> This is rarely useful as ampersands are now automatically escaped. See <a class="reference internal" href="#escape">escape</a> for more information.</div>
<p>Replaces ampersands with <tt class="docutils literal"><span class="pre">&amp;amp;</span></tt> entities.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">fix_ampersands</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Tom</span> <span class="pre">&amp;</span> <span class="pre">Jerry</span></tt>, the output will be <tt class="docutils literal"><span class="pre">Tom</span> <span class="pre">&amp;amp;</span> <span class="pre">Jerry</span></tt>.</p>
</div>
<div class="section" id="s-floatformat">
<span id="s-tfilter-floatformat"></span><span id="floatformat"></span><span id="tfilter-floatformat"></span><h3>floatformat<a class="headerlink" href="#floatformat" title="Permalink to this headline">¶</a></h3>
<p>When used without an argument, rounds a floating-point number to one decimal
place -- but only if there's a decimal part to be displayed. For example:</p>
<table class="docutils">
<colgroup>
<col width="26%">
<col width="57%">
<col width="17%">
</colgroup>
<thead valign="bottom">
<tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th>
<th class="head">Template</th>
<th class="head">Output</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.2</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.3</span></tt></td>
</tr>
</tbody>
</table>
<p>If used with a numeric integer argument, <tt class="docutils literal"><span class="pre">floatformat</span></tt> rounds a number to
that many decimal places. For example:</p>
<table class="docutils">
<colgroup>
<col width="24%">
<col width="57%">
<col width="20%">
</colgroup>
<thead valign="bottom">
<tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th>
<th class="head">Template</th>
<th class="head">Output</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.232</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.000</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.260</span></tt></td>
</tr>
</tbody>
</table>
<p>If the argument passed to <tt class="docutils literal"><span class="pre">floatformat</span></tt> is negative, it will round a number
to that many decimal places -- but only if there's a decimal part to be
displayed. For example:</p>
<table class="docutils">
<colgroup>
<col width="22%">
<col width="59%">
<col width="19%">
</colgroup>
<thead valign="bottom">
<tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th>
<th class="head">Template</th>
<th class="head">Output</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.232</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td>
<td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td>
<td><tt class="docutils literal"><span class="pre">34.260</span></tt></td>
</tr>
</tbody>
</table>
<p>Using <tt class="docutils literal"><span class="pre">floatformat</span></tt> with no argument is equivalent to using <tt class="docutils literal"><span class="pre">floatformat</span></tt>
with an argument of <tt class="docutils literal"><span class="pre">-1</span></tt>.</p>
</div>
<div class="section" id="s-force-escape">
<span id="s-tfilter-force_escape"></span><span id="force-escape"></span><span id="tfilter-force_escape"></span><h3>force_escape<a class="headerlink" href="#force-escape" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Applies HTML escaping to a string (see the <tt class="docutils literal"><span class="pre">escape</span></tt> filter for details).
This filter is applied <em>immediately</em> and returns a new, escaped string. This
is useful in the rare cases where you need multiple escaping or want to apply
other filters to the escaped results. Normally, you want to use the <tt class="docutils literal"><span class="pre">escape</span></tt>
filter.</p>
</div>
<div class="section" id="s-get-digit">
<span id="s-tfilter-get_digit"></span><span id="get-digit"></span><span id="tfilter-get_digit"></span><h3>get_digit<a class="headerlink" href="#get-digit" title="Permalink to this headline">¶</a></h3>
<p>Given a whole number, returns the requested digit, where 1 is the right-most
digit, 2 is the second-right-most digit, etc. Returns the original value for
invalid input (if input or argument is not an integer, or if argument is less
than 1). Otherwise, output is always an integer.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">get_digit</span><span class="s2">:"2"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">123456789</span></tt>, the output will be <tt class="docutils literal"><span class="pre">8</span></tt>.</p>
</div>
<div class="section" id="s-iriencode">
<span id="s-tfilter-iriendcode"></span><span id="iriencode"></span><span id="tfilter-iriendcode"></span><h3>iriencode<a class="headerlink" href="#iriencode" title="Permalink to this headline">¶</a></h3>
<p>Converts an IRI (Internationalized Resource Identifier) to a string that is
suitable for including in a URL. This is necessary if you're trying to use
strings containing non-ASCII characters in a URL.</p>
<p>It's safe to use this filter on a string that has already gone through the
<tt class="docutils literal"><span class="pre">urlencode</span></tt> filter.</p>
</div>
<div class="section" id="s-join">
<span id="s-tfilter-join"></span><span id="join"></span><span id="tfilter-join"></span><h3>join<a class="headerlink" href="#join" title="Permalink to this headline">¶</a></h3>
<p>Joins a list with a string, like Python's <tt class="docutils literal"><span class="pre">str.join(list)</span></tt></p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">join</span><span class="s2">:" // "</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></tt>, the output will be the string
<tt class="docutils literal"><span class="pre">"a</span> <span class="pre">//</span> <span class="pre">b</span> <span class="pre">//</span> <span class="pre">c"</span></tt>.</p>
</div>
<div class="section" id="s-last">
<span id="s-tfilter-last"></span><span id="last"></span><span id="tfilter-last"></span><h3>last<a class="headerlink" href="#last" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Returns the last item in a list.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">last</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be the string
<tt class="docutils literal"><span class="pre">"d"</span></tt>.</p>
</div>
<div class="section" id="s-length">
<span id="s-tfilter-length"></span><span id="length"></span><span id="tfilter-length"></span><h3>length<a class="headerlink" href="#length" title="Permalink to this headline">¶</a></h3>
<p>Returns the length of the value. This works for both strings and lists.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be <tt class="docutils literal"><span class="pre">4</span></tt>.</p>
</div>
<div class="section" id="s-length-is">
<span id="s-tfilter-length_is"></span><span id="length-is"></span><span id="tfilter-length_is"></span><h3>length_is<a class="headerlink" href="#length-is" title="Permalink to this headline">¶</a></h3>
<p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the value's length is the argument, or <tt class="xref docutils literal"><span class="pre">False</span></tt> otherwise.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length_is</span><span class="s2">:"4"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="section" id="s-linebreaks">
<span id="s-tfilter-linebreaks"></span><span id="linebreaks"></span><span id="tfilter-linebreaks"></span><h3>linebreaks<a class="headerlink" href="#linebreaks" title="Permalink to this headline">¶</a></h3>
<p>Replaces line breaks in plain text with appropriate HTML; a single
newline becomes an HTML line break (<tt class="docutils literal"><span class="pre">&lt;br</span> <span class="pre">/&gt;</span></tt>) and a new line
followed by a blank line becomes a paragraph break (<tt class="docutils literal"><span class="pre">&lt;/p&gt;</span></tt>).</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaks</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Joel\nis</span> <span class="pre">a</span> <span class="pre">slug</span></tt>, the output will be <tt class="docutils literal"><span class="pre">&lt;p&gt;Joel&lt;br</span> <span class="pre">/&gt;is</span> <span class="pre">a</span>
<span class="pre">slug&lt;/p&gt;</span></tt>.</p>
</div>
<div class="section" id="s-linebreaksbr">
<span id="s-tfilter-linebreaksbr"></span><span id="linebreaksbr"></span><span id="tfilter-linebreaksbr"></span><h3>linebreaksbr<a class="headerlink" href="#linebreaksbr" title="Permalink to this headline">¶</a></h3>
<p>Converts all newlines in a piece of plain text to HTML line breaks
(<tt class="docutils literal"><span class="pre">&lt;br</span> <span class="pre">/&gt;</span></tt>).</p>
</div>
<div class="section" id="s-linenumbers">
<span id="s-tfilter-linenumbers"></span><span id="linenumbers"></span><span id="tfilter-linenumbers"></span><h3>linenumbers<a class="headerlink" href="#linenumbers" title="Permalink to this headline">¶</a></h3>
<p>Displays text with line numbers.</p>
</div>
<div class="section" id="s-ljust">
<span id="s-tfilter-ljust"></span><span id="ljust"></span><span id="tfilter-ljust"></span><h3>ljust<a class="headerlink" href="#ljust" title="Permalink to this headline">¶</a></h3>
<p>Left-aligns the value in a field of a given width.</p>
<p><strong>Argument:</strong> field size</p>
</div>
<div class="section" id="s-lower">
<span id="s-tfilter-lower"></span><span id="lower"></span><span id="tfilter-lower"></span><h3>lower<a class="headerlink" href="#lower" title="Permalink to this headline">¶</a></h3>
<p>Converts a string into all lowercase.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Still</span> <span class="pre">MAD</span> <span class="pre">At</span> <span class="pre">Yoko</span></tt>, the output will be <tt class="docutils literal"><span class="pre">still</span> <span class="pre">mad</span> <span class="pre">at</span> <span class="pre">yoko</span></tt>.</p>
</div>
<div class="section" id="s-make-list">
<span id="s-tfilter-make_list"></span><span id="make-list"></span><span id="tfilter-make_list"></span><h3>make_list<a class="headerlink" href="#make-list" title="Permalink to this headline">¶</a></h3>
<p>Returns the value turned into a list. For an integer, it's a list of
digits. For a string, it's a list of characters.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">make_list</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the string <tt class="docutils literal"><span class="pre">"Joel"</span></tt>, the output would be the list
<tt class="docutils literal"><span class="pre">[u'J',</span> <span class="pre">u'o',</span> <span class="pre">u'e',</span> <span class="pre">u'l']</span></tt>. If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">123</span></tt>, the output will be the
list <tt class="docutils literal"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></tt>.</p>
</div>
<div class="section" id="s-phone2numeric">
<span id="s-tfilter-phone2numeric"></span><span id="phone2numeric"></span><span id="tfilter-phone2numeric"></span><h3>phone2numeric<a class="headerlink" href="#phone2numeric" title="Permalink to this headline">¶</a></h3>
<p>Converts a phone number (possibly containing letters) to its numerical
equivalent. For example, <tt class="docutils literal"><span class="pre">'800-COLLECT'</span></tt> will be converted to
<tt class="docutils literal"><span class="pre">'800-2655328'</span></tt>.</p>
<p>The input doesn't have to be a valid phone number. This will happily convert
any string.</p>
</div>
<div class="section" id="s-pluralize">
<span id="s-tfilter-pluralize"></span><span id="pluralize"></span><span id="tfilter-pluralize"></span><h3>pluralize<a class="headerlink" href="#pluralize" title="Permalink to this headline">¶</a></h3>
<p>Returns a plural suffix if the value is not 1. By default, this suffix is <tt class="docutils literal"><span class="pre">'s'</span></tt>.</p>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre>You have <span class="cp">{{</span> <span class="nv">num_messages</span> <span class="cp">}}</span> message<span class="cp">{{</span> <span class="nv">num_messages</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>For words that require a suffix other than <tt class="docutils literal"><span class="pre">'s'</span></tt>, you can provide an alternate
suffix as a parameter to the filter.</p>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre>You have <span class="cp">{{</span> <span class="nv">num_walruses</span> <span class="cp">}}</span> walrus<span class="cp">{{</span> <span class="nv">num_walrus</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:"es"</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>For words that don't pluralize by simple suffix, you can specify both a
singular and plural suffix, separated by a comma.</p>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre>You have <span class="cp">{{</span> <span class="nv">num_cherries</span> <span class="cp">}}</span> cherr<span class="cp">{{</span> <span class="nv">num_cherries</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:"y,ies"</span> <span class="cp">}}</span>.
</pre></div>
</div>
</div>
<div class="section" id="s-pprint">
<span id="s-tfilter-pprint"></span><span id="pprint"></span><span id="tfilter-pprint"></span><h3>pprint<a class="headerlink" href="#pprint" title="Permalink to this headline">¶</a></h3>
<p>A wrapper around <a class="reference external" href="http://docs.python.org/library/pprint.html">pprint.pprint</a> -- for debugging, really.</p>
</div>
<div class="section" id="s-random">
<span id="s-tfilter-random"></span><span id="random"></span><span id="tfilter-random"></span><h3>random<a class="headerlink" href="#random" title="Permalink to this headline">¶</a></h3>
<p>Returns a random item from the given list.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">random</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output could be <tt class="docutils literal"><span class="pre">"b"</span></tt>.</p>
</div>
<div class="section" id="s-removetags">
<span id="s-tfilter-removetags"></span><span id="removetags"></span><span id="tfilter-removetags"></span><h3>removetags<a class="headerlink" href="#removetags" title="Permalink to this headline">¶</a></h3>
<p>Removes a space-separated list of [X]HTML tags from the output.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">removetags</span><span class="s2">:"b span"</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"&lt;b&gt;Joel&lt;/b&gt;</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span> <span class="pre">&lt;span&gt;slug&lt;/span&gt;"</span></tt> the
output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p>
</div>
<div class="section" id="s-rjust">
<span id="s-tfilter-rjust"></span><span id="rjust"></span><span id="tfilter-rjust"></span><h3>rjust<a class="headerlink" href="#rjust" title="Permalink to this headline">¶</a></h3>
<p>Right-aligns the value in a field of a given width.</p>
<p><strong>Argument:</strong> field size</p>
</div>
<div class="section" id="s-safe">
<span id="s-tfilter-safe"></span><span id="safe"></span><span id="tfilter-safe"></span><h3>safe<a class="headerlink" href="#safe" title="Permalink to this headline">¶</a></h3>
<p>Marks a string as not requiring further HTML escaping prior to output. When
autoescaping is off, this filter has no effect.</p>
</div>
<div class="section" id="s-slice">
<span id="s-tfilter-slice"></span><span id="slice"></span><span id="tfilter-slice"></span><h3>slice<a class="headerlink" href="#slice" title="Permalink to this headline">¶</a></h3>
<p>Returns a slice of the list.</p>
<p>Uses the same syntax as Python's list slicing. See
<a class="reference external" href="http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice">http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice</a>
for an introduction.</p>
<p>Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">slice</span><span class="s2">:":2"</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-slugify">
<span id="s-tfilter-slugify"></span><span id="slugify"></span><span id="tfilter-slugify"></span><h3>slugify<a class="headerlink" href="#slugify" title="Permalink to this headline">¶</a></h3>
<p>Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and trailing
whitespace.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">slugify</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"joel-is-a-slug"</span></tt>.</p>
</div>
<div class="section" id="s-stringformat">
<span id="s-tfilter-stringformat"></span><span id="stringformat"></span><span id="tfilter-stringformat"></span><h3>stringformat<a class="headerlink" href="#stringformat" title="Permalink to this headline">¶</a></h3>
<p>Formats the variable according to the argument, a string formatting specifier.
This specifier uses Python string formatting syntax, with the exception that
the leading "%" is dropped.</p>
<p>See <a class="reference external" href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">http://docs.python.org/library/stdtypes.html#string-formatting-operations</a>
for documentation of Python string formatting</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">stringformat</span><span class="s2">:"s"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p>
</div>
<div class="section" id="s-striptags">
<span id="s-tfilter-striptags"></span><span id="striptags"></span><span id="tfilter-striptags"></span><h3>striptags<a class="headerlink" href="#striptags" title="Permalink to this headline">¶</a></h3>
<p>Strips all [X]HTML tags.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">striptags</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"&lt;b&gt;Joel&lt;/b&gt;</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span> <span class="pre">&lt;span&gt;slug&lt;/span&gt;"</span></tt>, the
output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p>
</div>
<div class="section" id="s-time">
<span id="s-tfilter-time"></span><span id="time"></span><span id="tfilter-time"></span><h3>time<a class="headerlink" href="#time" title="Permalink to this headline">¶</a></h3>
<p>Formats a time according to the given format (same as the <a class="reference internal" href="#now">now</a> tag).
The time filter will only accept parameters in the format string that relate
to the time of day, not the date (for obvious reasons). If you need to
format a date, use the <a class="reference internal" href="#date">date</a> filter.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"H:i"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt>, the output will be
the string <tt class="docutils literal"><span class="pre">"01:23"</span></tt>.</p>
<p>When used without a format string:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>...the formatting string defined in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-TIME_FORMAT"><tt class="xref docutils literal"><span class="pre">TIME_FORMAT</span></tt></a> setting will be
used.</p>
</div>
<div class="section" id="s-timesince">
<span id="s-tfilter-timesince"></span><span id="timesince"></span><span id="tfilter-timesince"></span><h3>timesince<a class="headerlink" href="#timesince" title="Permalink to this headline">¶</a></h3>
<p>Formats a date as the time since that date (e.g., "4 days, 6 hours").</p>
<p>Takes an optional argument that is a variable containing the date to use as
the comparison point (without the argument, the comparison point is <em>now</em>).
For example, if <tt class="docutils literal"><span class="pre">blog_date</span></tt> is a date instance representing midnight on 1
June 2006, and <tt class="docutils literal"><span class="pre">comment_date</span></tt> is a date instance for 08:00 on 1 June 2006,
then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">blog_date|timesince:comment_date</span> <span class="pre">}}</span></tt> would return "8 hours".</p>
<p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p>
<p>Minutes is the smallest unit used, and "0 minutes" will be returned for any
date that is in the future relative to the comparison point.</p>
</div>
<div class="section" id="s-timeuntil">
<span id="s-tfilter-timeuntil"></span><span id="timeuntil"></span><span id="tfilter-timeuntil"></span><h3>timeuntil<a class="headerlink" href="#timeuntil" title="Permalink to this headline">¶</a></h3>
<p>Similar to <tt class="docutils literal"><span class="pre">timesince</span></tt>, except that it measures the time from now until the
given date or datetime. For example, if today is 1 June 2006 and
<tt class="docutils literal"><span class="pre">conference_date</span></tt> is a date instance holding 29 June 2006, then
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">conference_date|timeuntil</span> <span class="pre">}}</span></tt> will return "4 weeks".</p>
<p>Takes an optional argument that is a variable containing the date to use as
the comparison point (instead of <em>now</em>). If <tt class="docutils literal"><span class="pre">from_date</span></tt> contains 22 June
2006, then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">conference_date|timeuntil:from_date</span> <span class="pre">}}</span></tt> will return "1 week".</p>
<p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p>
<p>Minutes is the smallest unit used, and "0 minutes" will be returned for any
date that is in the past relative to the comparison point.</p>
</div>
<div class="section" id="s-title">
<span id="s-tfilter-title"></span><span id="title"></span><span id="tfilter-title"></span><h3>title<a class="headerlink" href="#title" title="Permalink to this headline">¶</a></h3>
<p>Converts a string into titlecase.</p>
</div>
<div class="section" id="s-truncatewords">
<span id="s-tfilter-truncatewords"></span><span id="truncatewords"></span><span id="tfilter-truncatewords"></span><h3>truncatewords<a class="headerlink" href="#truncatewords" title="Permalink to this headline">¶</a></h3>
<p>Truncates a string after a certain number of words.</p>
<p><strong>Argument:</strong> Number of words to truncate after</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatewords</span><span class="o">:</span><span class="m">2</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">..."</span></tt>.</p>
</div>
<div class="section" id="s-truncatewords-html">
<span id="s-tfilter-truncatewords_html"></span><span id="truncatewords-html"></span><span id="tfilter-truncatewords_html"></span><h3>truncatewords_html<a class="headerlink" href="#truncatewords-html" title="Permalink to this headline">¶</a></h3>
<p>Similar to <tt class="docutils literal"><span class="pre">truncatewords</span></tt>, except that it is aware of HTML tags. Any tags
that are opened in the string and not closed before the truncation point, are
closed immediately after the truncation.</p>
<p>This is less efficient than <tt class="docutils literal"><span class="pre">truncatewords</span></tt>, so should only be used when it
is being passed HTML text.</p>
</div>
<div class="section" id="s-unordered-list">
<span id="s-tfilter-unordered_list"></span><span id="unordered-list"></span><span id="tfilter-unordered_list"></span><h3>unordered_list<a class="headerlink" href="#unordered-list" title="Permalink to this headline">¶</a></h3>
<p>Recursively takes a self-nested list and returns an HTML unordered list --
WITHOUT opening and closing &lt;ul&gt; tags.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> The format accepted by <tt class="docutils literal"><span class="pre">unordered_list</span></tt> has changed to be easier to understand.</div>
<p>The list is assumed to be in the proper format. For example, if <tt class="docutils literal"><span class="pre">var</span></tt> contains
<tt class="docutils literal"><span class="pre">['States',</span> <span class="pre">['Kansas',</span> <span class="pre">['Lawrence',</span> <span class="pre">'Topeka'],</span> <span class="pre">'Illinois']]</span></tt>, then
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">var|unordered_list</span> <span class="pre">}}</span></tt> would return:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;li&gt;</span>States
<span class="nt">&lt;ul&gt;</span>
        <span class="nt">&lt;li&gt;</span>Kansas
        <span class="nt">&lt;ul&gt;</span>
                <span class="nt">&lt;li&gt;</span>Lawrence<span class="nt">&lt;/li&gt;</span>
                <span class="nt">&lt;li&gt;</span>Topeka<span class="nt">&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>
        <span class="nt">&lt;/li&gt;</span>
        <span class="nt">&lt;li&gt;</span>Illinois<span class="nt">&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>
<span class="nt">&lt;/li&gt;</span>
</pre></div>
</div>
<p>Note: the previous more restrictive and verbose format is still supported:
<tt class="docutils literal"><span class="pre">['States',</span> <span class="pre">[['Kansas',</span> <span class="pre">[['Lawrence',</span> <span class="pre">[]],</span> <span class="pre">['Topeka',</span> <span class="pre">[]]]],</span> <span class="pre">['Illinois',</span> <span class="pre">[]]]]</span></tt>,</p>
</div>
<div class="section" id="s-upper">
<span id="s-tfilter-upper"></span><span id="upper"></span><span id="tfilter-upper"></span><h3>upper<a class="headerlink" href="#upper" title="Permalink to this headline">¶</a></h3>
<p>Converts a string into all uppercase.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">upper</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"JOEL</span> <span class="pre">IS</span> <span class="pre">A</span> <span class="pre">SLUG"</span></tt>.</p>
</div>
<div class="section" id="s-urlencode">
<span id="s-tfilter-urlencode"></span><span id="urlencode"></span><span id="tfilter-urlencode"></span><h3>urlencode<a class="headerlink" href="#urlencode" title="Permalink to this headline">¶</a></h3>
<p>Escapes a value for use in a URL.</p>
</div>
<div class="section" id="s-urlize">
<span id="s-tfilter-urlize"></span><span id="urlize"></span><span id="tfilter-urlize"></span><h3>urlize<a class="headerlink" href="#urlize" title="Permalink to this headline">¶</a></h3>
<p>Converts URLs in plain text into clickable links.</p>
<p>Note that if <tt class="docutils literal"><span class="pre">urlize</span></tt> is applied to text that already contains HTML markup,
things won't work as expected. Apply this filter only to <em>plain</em> text.</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlize</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></tt>, the output will be
<tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">&lt;a</span>
<span class="pre">href="http://www.djangoproject.com"&gt;www.djangoproject.com&lt;/a&gt;"</span></tt>.</p>
</div>
<div class="section" id="s-urlizetrunc">
<span id="s-tfilter-urlizetrunc"></span><span id="urlizetrunc"></span><span id="tfilter-urlizetrunc"></span><h3>urlizetrunc<a class="headerlink" href="#urlizetrunc" title="Permalink to this headline">¶</a></h3>
<p>Converts URLs into clickable links, truncating URLs longer than the given
character limit.</p>
<p>As with <a class="reference internal" href="#urlize">urlize</a>, this filter should only be applied to <em>plain</em> text.</p>
<p><strong>Argument:</strong> Length to truncate URLs to</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlizetrunc</span><span class="o">:</span><span class="m">15</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></tt>, the output would be
<tt class="docutils literal"><span class="pre">'Check</span> <span class="pre">out</span> <span class="pre">&lt;a</span>
<span class="pre">href="http://www.djangoproject.com"&gt;www.djangopr...&lt;/a&gt;'</span></tt>.</p>
</div>
<div class="section" id="s-wordcount">
<span id="s-tfilter-wordcount"></span><span id="wordcount"></span><span id="tfilter-wordcount"></span><h3>wordcount<a class="headerlink" href="#wordcount" title="Permalink to this headline">¶</a></h3>
<p>Returns the number of words.</p>
</div>
<div class="section" id="s-wordwrap">
<span id="s-tfilter-wordwrap"></span><span id="wordwrap"></span><span id="tfilter-wordwrap"></span><h3>wordwrap<a class="headerlink" href="#wordwrap" title="Permalink to this headline">¶</a></h3>
<p>Wraps words at specified line length.</p>
<p><strong>Argument:</strong> number of characters at which to wrap the text</p>
<p>For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordwrap</span><span class="o">:</span><span class="m">5</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug</span></tt>, the output would be:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Joel
is a
slug
</pre></div>
</div>
</div>
<div class="section" id="s-yesno">
<span id="s-tfilter-yesno"></span><span id="yesno"></span><span id="tfilter-yesno"></span><h3>yesno<a class="headerlink" href="#yesno" title="Permalink to this headline">¶</a></h3>
<p>Given a string mapping values for true, false and (optionally) None,
returns one of those strings according to the value:</p>
<table class="docutils">
<colgroup>
<col width="15%">
<col width="33%">
<col width="52%">
</colgroup>
<thead valign="bottom">
<tr><th class="head">Value</th>
<th class="head">Argument</th>
<th class="head">Outputs</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="xref docutils literal"><span class="pre">True</span></tt></td>
<td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td>
<td><tt class="docutils literal"><span class="pre">yeah</span></tt></td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">False</span></tt></td>
<td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td>
<td><tt class="docutils literal"><span class="pre">no</span></tt></td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">None</span></tt></td>
<td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td>
<td><tt class="docutils literal"><span class="pre">maybe</span></tt></td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">None</span></tt></td>
<td><tt class="docutils literal"><span class="pre">"yeah,no"</span></tt></td>
<td><tt class="docutils literal"><span class="pre">"no"</span></tt> (converts None to False
if no mapping for None is given)</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="s-other-tags-and-filter-libraries">
<span id="other-tags-and-filter-libraries"></span><h2>Other tags and filter libraries<a class="headerlink" href="#other-tags-and-filter-libraries" title="Permalink to this headline">¶</a></h2>
<p>Django comes with a couple of other template-tag libraries that you have to
enable explicitly in your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> setting and enable in your
template with the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></tt> tag.</p>
<div class="section" id="s-django-contrib-humanize">
<span id="django-contrib-humanize"></span><h3>django.contrib.humanize<a class="headerlink" href="#django-contrib-humanize" title="Permalink to this headline">¶</a></h3>
<p>A set of Django template filters useful for adding a "human touch" to data. See
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/contrib/humanize/#ref-contrib-humanize"><em>django.contrib.humanize</em></a>.</p>
</div>
<div class="section" id="s-django-contrib-markup">
<span id="django-contrib-markup"></span><h3>django.contrib.markup<a class="headerlink" href="#django-contrib-markup" title="Permalink to this headline">¶</a></h3>
<p>A collection of template filters that implement these common markup languages:</p>
<ul class="simple">
<li>Textile</li>
<li>Markdown</li>
<li>ReST (ReStructured Text)</li>
</ul>
<p>See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/contrib/#ref-contrib-markup"><em>markup</em></a>.</p>
</div>
<div class="section" id="s-django-contrib-webdesign">
<span id="django-contrib-webdesign"></span><h3>django.contrib.webdesign<a class="headerlink" href="#django-contrib-webdesign" title="Permalink to this headline">¶</a></h3>
<p>A collection of template tags that can be useful while designing a website,
such as a generator of Lorem Ipsum text. See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/contrib/webdesign/#ref-contrib-webdesign"><em>django.contrib.webdesign</em></a>.</p>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Built-in template tags and filters</a><ul>
<li><a class="reference external" href="#built-in-tag-reference">Built-in tag reference</a><ul>
<li><a class="reference external" href="#autoescape">autoescape</a></li>
<li><a class="reference external" href="#block">block</a></li>
<li><a class="reference external" href="#comment">comment</a></li>
<li><a class="reference external" href="#cycle">cycle</a></li>
<li><a class="reference external" href="#debug">debug</a></li>
<li><a class="reference external" href="#extends">extends</a></li>
<li><a class="reference external" href="#filter">filter</a></li>
<li><a class="reference external" href="#firstof">firstof</a></li>
<li><a class="reference external" href="#for">for</a></li>
<li><a class="reference external" href="#if">if</a></li>
<li><a class="reference external" href="#ifchanged">ifchanged</a></li>
<li><a class="reference external" href="#ifequal">ifequal</a></li>
<li><a class="reference external" href="#ifnotequal">ifnotequal</a></li>
<li><a class="reference external" href="#include">include</a></li>
<li><a class="reference external" href="#load">load</a></li>
<li><a class="reference external" href="#now">now</a></li>
<li><a class="reference external" href="#regroup">regroup</a></li>
<li><a class="reference external" href="#spaceless">spaceless</a></li>
<li><a class="reference external" href="#ssi">ssi</a></li>
<li><a class="reference external" href="#templatetag">templatetag</a></li>
<li><a class="reference external" href="#url">url</a></li>
<li><a class="reference external" href="#widthratio">widthratio</a></li>
<li><a class="reference external" href="#with">with</a></li>
</ul>
</li>
<li><a class="reference external" href="#built-in-filter-reference">Built-in filter reference</a><ul>
<li><a class="reference external" href="#add">add</a></li>
<li><a class="reference external" href="#addslashes">addslashes</a></li>
<li><a class="reference external" href="#capfirst">capfirst</a></li>
<li><a class="reference external" href="#center">center</a></li>
<li><a class="reference external" href="#cut">cut</a></li>
<li><a class="reference external" href="#date">date</a></li>
<li><a class="reference external" href="#default">default</a></li>
<li><a class="reference external" href="#default-if-none">default_if_none</a></li>
<li><a class="reference external" href="#dictsort">dictsort</a></li>
<li><a class="reference external" href="#dictsortreversed">dictsortreversed</a></li>
<li><a class="reference external" href="#divisibleby">divisibleby</a></li>
<li><a class="reference external" href="#escape">escape</a></li>
<li><a class="reference external" href="#escapejs">escapejs</a></li>
<li><a class="reference external" href="#filesizeformat">filesizeformat</a></li>
<li><a class="reference external" href="#first">first</a></li>
<li><a class="reference external" href="#fix-ampersands">fix_ampersands</a></li>
<li><a class="reference external" href="#floatformat">floatformat</a></li>
<li><a class="reference external" href="#force-escape">force_escape</a></li>
<li><a class="reference external" href="#get-digit">get_digit</a></li>
<li><a class="reference external" href="#iriencode">iriencode</a></li>
<li><a class="reference external" href="#join">join</a></li>
<li><a class="reference external" href="#last">last</a></li>
<li><a class="reference external" href="#length">length</a></li>
<li><a class="reference external" href="#length-is">length_is</a></li>
<li><a class="reference external" href="#linebreaks">linebreaks</a></li>
<li><a class="reference external" href="#linebreaksbr">linebreaksbr</a></li>
<li><a class="reference external" href="#linenumbers">linenumbers</a></li>
<li><a class="reference external" href="#ljust">ljust</a></li>
<li><a class="reference external" href="#lower">lower</a></li>
<li><a class="reference external" href="#make-list">make_list</a></li>
<li><a class="reference external" href="#phone2numeric">phone2numeric</a></li>
<li><a class="reference external" href="#pluralize">pluralize</a></li>
<li><a class="reference external" href="#pprint">pprint</a></li>
<li><a class="reference external" href="#random">random</a></li>
<li><a class="reference external" href="#removetags">removetags</a></li>
<li><a class="reference external" href="#rjust">rjust</a></li>
<li><a class="reference external" href="#safe">safe</a></li>
<li><a class="reference external" href="#slice">slice</a></li>
<li><a class="reference external" href="#slugify">slugify</a></li>
<li><a class="reference external" href="#stringformat">stringformat</a></li>
<li><a class="reference external" href="#striptags">striptags</a></li>
<li><a class="reference external" href="#time">time</a></li>
<li><a class="reference external" href="#timesince">timesince</a></li>
<li><a class="reference external" href="#timeuntil">timeuntil</a></li>
<li><a class="reference external" href="#title">title</a></li>
<li><a class="reference external" href="#truncatewords">truncatewords</a></li>
<li><a class="reference external" href="#truncatewords-html">truncatewords_html</a></li>
<li><a class="reference external" href="#unordered-list">unordered_list</a></li>
<li><a class="reference external" href="#upper">upper</a></li>
<li><a class="reference external" href="#urlencode">urlencode</a></li>
<li><a class="reference external" href="#urlize">urlize</a></li>
<li><a class="reference external" href="#urlizetrunc">urlizetrunc</a></li>
<li><a class="reference external" href="#wordcount">wordcount</a></li>
<li><a class="reference external" href="#wordwrap">wordwrap</a></li>
<li><a class="reference external" href="#yesno">yesno</a></li>
</ul>
</li>
<li><a class="reference external" href="#other-tags-and-filter-libraries">Other tags and filter libraries</a><ul>
<li><a class="reference external" href="#django-contrib-humanize">django.contrib.humanize</a></li>
<li><a class="reference external" href="#django-contrib-markup">django.contrib.markup</a></li>
<li><a class="reference external" href="#django-contrib-webdesign">django.contrib.webdesign</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/ref/templates/">Template reference</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/ref/templates/api/">The Django template language: For Python programmers</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/ref/">API Reference</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/ref/templates/">Template reference</a>
          
          <ul><li>Built-in template tags and filters</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Built-in%20template%20tags%20and%20filters%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>